import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public int[] dx = {0, 0, -1, 1};
    public int[] dy = {1, -1, 0, 0};
    boolean[][] vis = new boolean[301][301];
    int m, n;
    public int num = 0;

    public int numIslands(char[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int count = 0;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(grid[i][j] == '1' && !vis[i][j]){
                    count++;
                    markIland(grid, i, j);
                }
            }
        }
        return count;
    }
    private void markIland(char[][] grid, int x, int y){
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[] {x, y});
        vis[x][y] = true;
        while(!queue.isEmpty()){
            int[] point = queue.poll();
            num++;
            int a = point[0];
            int b = point[1];
            //vis[a][b] = true;
            System.out.println("a=" + a + " b=" + b);
            for(int i = 0; i < 4; i++){
                int xx = a + dx[i];
                int yy = b + dy[i];
                if(xx >= 0 && xx < m && yy >= 0 && yy < n && grid[xx][yy] == '1' && !vis[xx][yy]){
                    queue.offer(new int[] {xx, yy});
                    vis[xx][yy] = true;
                }
            }
        }
    }
}